<?php
/**
 * Task processing queue
 * @author ice<iceinto@mallog.com.cn>
 */
class QueueModel extends BaseModel {
	/**
	 * Get one
	 *
	 * @param $id int
	 * @return array
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getOneById($id, $fields = 'status') {
		return $this->getById ( $id, $fields );
	}

	/**
	 * 返回任务状态说明
	 * 任务状态说明:1等待处理、2进行中、3需求退回、4等待审核、5审核退回、6审核通过、7等待发送、8发送完成
	 *
	 * @param $status int
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function getStatus($status) {
		$message ['1'] = '等待处理';
		$message ['2'] = '已经分配';
		$message ['3'] = '进行中';
		$message ['4'] = '需求退回';
		$message ['5'] = '需求退回已经处理';
		$message ['6'] = '等待审核';
		$message ['7'] = '审核退回';
		$message ['8'] = '审核退回已经处理';
		$message ['9'] = '审核通过';

		$message ['100'] = '推送成功';
		if ($message [$status]) {
			return $message [$status];
		} else {
			return $message ['1'];
		}
	}

	/**
	 * update status
	 *
	 * @param $id int
	 * @param $status int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function updateStatus($tid, $status, $auditor = null) {
		$data = array ();
		$data ['tid'] = $tid;
		$data ['status'] = $status;
		if ($auditor) {
			$data ['auditor'] = $auditor;
		}
		$data ['update_time'] = time ();
		return $this->save ( $data );
	}

	/**
	 * Add one
	 *
	 * @param $task array
	 * @param $executor int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function addOne($task, $executor) {
		$data = array ();
		$data ['tid'] = $task['id'];
		$data ['creator'] = $task['uid'];
		$data ['executor'] = $executor;
		$data ['mid'] = $task['mid'];
		$data ['fid'] = $task['fid'];
		$data ['bid'] = $task['bid'];
		$data ['create_time'] = time ();
		$data ['status'] = 2;
		return $this->add ( $data );
	}

	/**
	 * Update one
	 *
	 * @param $data arrat
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function updateOne($data) {
		$data ['update_time'] = time ();
		return $this->save ( $data );
	}

	/**
	 * Access to current task
	 *
	 * @param $uid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getNowTask($uid) {
		$map = array ();
		$map['executor'] = $uid;
		$map ['status'] = array (array ('EQ', 2 ), array ('EQ', 3 ), 'OR' );
		$taskId = $this->field ( 'tid' )->where ( $map )->find ();
		if ($taskId ['tid']) {
			$auditReturn = D ( 'TaskAuditReturn' )->getReturnOne ( $taskId ['tid'] );
			$task = D ( 'Task' )->field('id,title,bid')->find ( $taskId ['tid'] );
			if ($auditReturn) {
				$task ['auditReturn'] = $auditReturn;
			}
			return $task;
		}
		return null;
	}

	/**
	 * Access to current state of the number of tasks
	 *
	 * @param $status int
	 *            status
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getStatusCount($status, $executor = 0) {
		$map = array ();
		$map ['status'] = $status;
		if($executor){
			$map['executor'] = $executor;
		}
		return $this->where ( $map )->count ();
	}

	public function getCreatorStatusCount($creator, $status, $mid = 0) {
		$map = array ();
		$map ['creator'] = $creator;
		$map ['status'] = $status;
		if($mid){
			$map['mid'] = $mid;
		}
		return $this->where ( $map )->count ();
	}
	/**
	 *
	 * @param $tid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getStatusById($tid) {
		$queue = $this->getOneById ( $tid );
		return $queue ['status'];
	}

	/**
	 * According to the state of the task assigned a task
	 *
	 * @param $uid int
	 * @param $status int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function allotOneByStatus($uid, $status) {
		$map = array ();
		$data = array ();

		$map ['executor'] = $uid;
		$map ['status'] = $status;

		$data ['update_time'] = time ();
		$data ['status'] = 2;

		$queue = $this->where ( $map )->field ( 'tid' )->find ();
		if (! $queue) {
			return null;
		}
		$data ['tid'] = $queue ['tid'];

		if ($this->save ( $data )) {
			return D ( 'Task' )->getOneById ( $queue ['tid'] );
		}
	}

	/**
	 * Waiting for audit for the task
	 *
	 * @param $uid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getWaitingAuditList($uid) {
		$map = array ();
		$MallModel = D('Mall');
		$map ['status'] = 6;
		$tasks = $this->field ( 'tid,bid' )->where ( $map )->select ();
		if ($tasks) {
			$TaskModel = D ( 'Task' );
			foreach ( $tasks as $k => $v ) {
				$task = $TaskModel->getById ( $v ['tid'], 'id,title' );
				$tasks [$k] = array_merge ( $tasks [$k], $task );
				$tasks [$k]['brand'] = $MallModel->getBrand($v['bid']);
			}
		}
		return $tasks;
	}
}
?>